﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace hkgoldenhighlight
{
	public partial class ScreenSaverForm : Form
	{
		#region Win32 API functions

        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll")]
        static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);
        
        [DllImport("winmm.dll")]
		private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
		#endregion
		
		private bool hasBeenExited = false;
		
		private Point mouseLocation;
    	private bool previewMode = false;
    	private int minY = 0;
		private int maxY = 0;
		private int maxX = 0;
		private int minX = 0;
		
		private bool CanShow = false;
		
		internal System.Windows.Forms.PictureBox[] picturebox = new PictureBox[0];
    			
		public ScreenSaverForm(/*Rectangle Bounds*/)
		{
			if (!hasBeenExited) {
				InfFile.LoadFromInfFile();
				
				InitializeComponent();
				#region
				//http://efreedom.com/Question/1-1930444/Windows-Screensaver-Multi-Monitor-Problem
				foreach (Screen screen in Screen.AllScreens)
				{
			        // Find the bounds of all screens attached to the system
			
			        if (screen.Bounds.Left < minX)
		                minX = screen.Bounds.Left;
			
			        if (screen.Bounds.Width > maxX)
		                maxX = screen.Bounds.Right;
			
			        if (screen.Bounds.Bottom < minY)
		                minY = screen.Bounds.Top;
			
			        if (screen.Bounds.Height > maxY)
		                maxY = screen.Bounds.Bottom;
				}
				
				// Set the location and size of the form, so that it
				// fills the bounds of all screens attached to the system
				
				Location = new Point(minX, minY);
				Height = maxY - minY;
				Width = maxX - minX;
				#endregion
			}
		}
		
		public void AddPictureBoxes()
		{
			/*if (!InfFile.DoesHaveTheFile || !InfFile.LoadBGMFirst || CanShow) {
				int i; for (i=0; i<Screen.AllScreens.Length; i++) {
					Array.Resize<PictureBox>(ref picturebox, picturebox.Length+1);
					
					PictureBox NewPBox = new PictureBox();
					NewPBox = pBox;
					NewPBox.Bounds = Screen.AllScreens[i].Bounds;
					NewPBox.Visible = true;
					
					this.Controls.Add(NewPBox);
					NewPBox.BringToFront();
				}
			}*/
			switch (Screen.AllScreens.Length) {
				case 8:
					pBox7.Bounds = Screen.AllScreens[7].Bounds;
					pBox7.Visible = true;
					goto case 7;
				case 7:
					pBox6.Bounds = Screen.AllScreens[6].Bounds;
					pBox6.Visible = true;
					goto case 6;
				case 6:
					pBox5.Bounds = Screen.AllScreens[5].Bounds;
					pBox5.Visible = true;
					goto case 5;
				case 5:
					pBox4.Bounds = Screen.AllScreens[4].Bounds;
					pBox4.Visible = true;
					goto case 4;
				case 4:
					pBox3.Bounds = Screen.AllScreens[3].Bounds;
					pBox3.Visible = true;
					goto case 3;
				case 3:
					pBox2.Bounds = Screen.AllScreens[2].Bounds;
					pBox2.Visible = true;
					goto case 2;
				case 2:
					pBox1.Bounds = Screen.AllScreens[1].Bounds;
					pBox1.Visible = true;
					goto case 1;
				case 1:
					pBox0.Bounds = Screen.AllScreens[0].Bounds;
					pBox0.Visible = true;
					break;
				default:
					goto case 8;
			}
			
			Cursor.Hide();
			TopMost = true;
		}
		
		public ScreenSaverForm(IntPtr PreviewWndHandle)
        {
            InitializeComponent();

            // Set the preview window as the parent of this window
            SetParent(this.Handle, PreviewWndHandle);

            // Make this a child window so it will close when the parent dialog closes
            SetWindowLong(this.Handle, -16, new IntPtr(GetWindowLong(this.Handle, -16) | 0x40000000));

            // Place our window inside the parent
            Rectangle ParentRect;
            GetClientRect(PreviewWndHandle, out ParentRect);
            Size = ParentRect.Size;
            Location = new Point(0, 0);
            
            pBox0.Dock = DockStyle.Fill;
            pBox0.Visible = true;

            previewMode = true;
        }
		
		private void ScreenSaverForm_Load(object sender, EventArgs e)
		{
			if (!previewMode)
			{
				AddPictureBoxes();
				if (InfFile.DoesHaveTheFile && InfFile.BackgroundMusic != null && System.IO.File.Exists(InfFile.BackgroundMusic)) {
					axWindowsMediaPlayer1.URL = @InfFile.BackgroundMusic;
					axWindowsMediaPlayer1.Ctlcontrols.play(); 
				}
			}
		}
		
		private void ScreenSaverForm_MouseMove(object sender, MouseEventArgs e)
		{
			if (!previewMode)
			{
				if (!mouseLocation.IsEmpty)
				{
					// Terminate if mouse is moved a significant distance
					if (Math.Abs(mouseLocation.X - e.X) > 5 || Math.Abs(mouseLocation.Y - e.Y) > 5)
					{
						Application.Exit();
						hasBeenExited = true;
					}
				}
			
				// Update current mouse location
				mouseLocation = e.Location;	
			}
		}
		
		private void ScreenSaverForm_MouseClick(object sender, MouseEventArgs e)
		{
			if (!previewMode)
			{
				Application.Exit();
				hasBeenExited = true;
			}
		}
		
		private void ScreenSaverForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!previewMode)
			{
				Application.Exit();
				hasBeenExited = true;
			}
		}
		
		private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) 
		{ 
			DebugLabel.Text = axWindowsMediaPlayer1.playState.ToString();
			if (!hasBeenExited) {
				if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
			     { 
			     	axWindowsMediaPlayer1.Ctlcontrols.play();
			     }
			     else if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying) {
					if (InfFile.LoadBGMFirst && !CanShow) {
						CanShow = true;
			     		AddPictureBoxes();
			     	}
			     }
			}
		}
	}
}
